package com.carcontrol.rotate;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;


import com.carcontrol.rotate.base.BaseFragment;
import com.carcontrol.rotate.utils.CRC16;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//空调
public class Fragment_Sixth extends BaseFragment {
    public static String TAG = "6";

    private FrameLayout v;
    private Context context;

    private GifImageView iv_route;
    private GifDrawable gifDrawable;

    private TextView tv_temp;   //空调温度
    private TextView tv_wind;   //风速

    float temp = 20;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    ImageView iv_s1_off;
    ImageView iv_s2_off;
    ImageView iv_s3_off;
    ImageView iv_s4_off;
    ImageView iv_s5_off;
    ImageView iv_s6_off;
    ImageView iv_s7_off;
    ImageView iv_s0_off;


    ImageView iv_s1_on;
    ImageView iv_s2_on;
    ImageView iv_s3_on;
    ImageView iv_s4_on;
    ImageView iv_s5_on;
    ImageView iv_s6_on;
    ImageView iv_s7_on;
    ImageView iv_sauto_on;

    Object[] gifObj;
    Object[] tempObj;
    Object[] windObj;

    Object[] allParam = new Object[]{

//            new Object[]{"iv", new Rect(373*width/width0-width_d, 151*height/height0-height_d, 777*width/width0, 285*height/height0), R.mipmap.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

//            new Object[]{"iv", new Rect(407*width/width0-width_d, 164*height/height0-height_d, 307*width/width0, 71*height/height0), R.mipmap.wind_gif_plate, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            //风动画
            new Object[]{"gif", new Rect(391*width/width0-width_d, 152*height/height0-height_d, 339*width/width0, 94*height/height0), R.mipmap.wind_1_gif, false, ""},

            //温度表
            new Object[]{"iv", new Rect(485*width/width0-width_d, 255*height/height0-height_d, 189*width/width0, 189*height/height0), R.mipmap.temp_plate, false, "", "", R.mipmap.s0_on},
            //风表
            new Object[]{"iv", new Rect(795*width/width0-width_d, 178*height/height0-height_d, 267*width/width0, 267*height/height0), R.mipmap.wind_plate, false, "", "", R.mipmap.s0_on},

//            new Object[]{"iv", new Rect(597*width/width0-width_d, 303*height/height0-height_d, 80*width/width0, 80*height/height0), R.mipmap.temp_round, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(602*width/width0-width_d, 308*height/height0-height_d, 69*width/width0, 69*height/height0), R.mipmap.temp_round2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            //温度-
            new Object[]{"iv", new Rect(476*width/width0-width_d, 350*height/height0-height_d, 101*width/width0, 106*height/height0), R.mipmap.reduce_off, true, "6110", "", R.mipmap.reduce_on},
            //温度+
            new Object[]{"iv", new Rect(580*width/width0-width_d, 350*height/height0-height_d, 101*width/width0, 106*height/height0), R.mipmap.plus_off, true, "6210", "", R.mipmap.plus_on},

            //温度值
            new Object[]{"tv", new Rect(558*width/width0-width_d, 307*height/height0-height_d, 0, 0), "温度", 20, Color.WHITE, false, "", 20f},

//            new Object[]{"tv", new Rect(931*width/width0-width_d, 344*height/height0-height_d, 0, 0), "1", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(891*width/width0-width_d, 310*height/height0-height_d, 0, 0), "2", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(891*width/width0-width_d, 254*height/height0-height_d, 0, 0), "3", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(933*width/width0-width_d, 223*height/height0-height_d, 0, 0), "4", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(982*width/width0-width_d, 223*height/height0-height_d, 0, 0), "5", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(1022*width/width0-width_d, 255*height/height0-height_d, 0, 0), "6", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(1022*width/width0-width_d, 310*height/height0-height_d, 0, 0), "7", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"tv", new Rect(969*width/width0-width_d, 351*height/height0-height_d, 0, 0), "AUTO", 12, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            //风速0
            new Object[]{"iv", new Rect(939*width/width0-width_d, 361*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s0_off, true, "6310", "0", R.mipmap.s0_on},
            //风速1
            new Object[]{"iv", new Rect(880*width/width0-width_d, 362*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s1_off, true, "6410", "1", R.mipmap.s1_on},
            //风速2
            new Object[]{"iv", new Rect(837*width/width0-width_d, 318*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s2_off, true, "6510", "2", R.mipmap.s2_on},
            //风速3
            new Object[]{"iv", new Rect(838*width/width0-width_d, 262*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s3_off, true, "6610", "3", R.mipmap.s3_on},
            //风速4
            new Object[]{"iv", new Rect(880*width/width0-width_d, 217*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s4_off, true, "6710", "4", R.mipmap.s4_on},
            //风速5
            new Object[]{"iv", new Rect(939*width/width0-width_d, 217*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s5_off, true, "6810", "5", R.mipmap.s5_on},
            //风速6
            new Object[]{"iv", new Rect(981*width/width0-width_d, 260*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s6_off, true, "6910", "6", R.mipmap.s6_on},
            //风速7
            new Object[]{"iv", new Rect(980*width/width0-width_d, 318*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s7_off, true, "6A10", "7", R.mipmap.s7_on},


//            new Object[]{"iv", new Rect(872*width/width0-width_d, 364*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s1_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(809*width/width0-width_d, 318*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s2_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(809*width/width0-width_d, 255*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s3_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(873*width/width0-width_d, 192*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s4_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(941*width/width0-width_d, 192*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s5_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(984*width/width0-width_d, 255*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s6_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(985*width/width0-width_d, 317*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s7_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
//            new Object[]{"iv", new Rect(924*width/width0-width_d, 364*height/height0-height_d, 40*width/width0, 45*height/height0), R.mipmap.s0_on, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},


//            new Object[]{"iv", new Rect(855*width/width0-width_d, 250*height/height0-height_d, 146*width/width0, 173*height/height0), R.mipmap.switch_off, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            //风速值
            new Object[]{"tv", new Rect(917*width/width0-width_d, 289*height/height0-height_d, 0, 0), "风速", 30, Color.BLUE, false, "", 0},
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_Sixth===0", v+"==="+width_d+"==="+height_d);

        initView();

//        isThread = true;
//        loopRead();

        return v;
    }

    protected void doAction1(String myDevice, String myStatus){
        Log.e("f6===doAction", myDevice+"==="+myStatus);

        if(myDevice.equals("6001")){    //6001
//            temp = Integer.parseInt(myStatus);

            int fir = Integer.parseInt(CRC16.convert16(myStatus.substring(2, 3)));
            int sec = Integer.parseInt(CRC16.convert16(myStatus.substring(3)));
            temp = fir * 16 + sec;


            Log.e("f6===doAction1", "==="+temp);

            tv_temp.setText(temp+"℃");

            if(temp>22){
                tv_temp.setText("HI");
            }else if(temp<18){
                tv_temp.setText("LO");
            }

            tempObj[7] = temp;

        }else if(myDevice.equals("6002")){    //6002
//            int wind = Integer.parseInt(myStatus);

            int fir = Integer.parseInt(CRC16.convert16(myStatus.substring(2, 3)));
            int sec = Integer.parseInt(CRC16.convert16(myStatus.substring(3)));
            int wind = fir * 16 + sec;

            tv_wind.setText(wind+"");
            windObj[7] = wind;

            if(wind>0){  //TODO
                iv_route.setImageResource(R.mipmap.wind_1_gif);
                gifDrawable.start();

                gifObj[3] = true;
            }else{
                iv_route.setImageResource(R.mipmap.wind_gif_off);
                gifDrawable.stop();

                gifObj[3] = false;
            }
        }

//        if(imgId==R.mipmap.reduce_off){
//            temp -= 0.5;
//            tv_temp.setText(temp+"℃");
//
//            uartJnit.getInstance().sendCmds(toCmd("6110"));
//        }else if(imgId==R.mipmap.plus_off){
//            temp += 0.5;
//            tv_temp.setText(temp+"℃");
//
//            uartJnit.getInstance().sendCmds(toCmd("6210"));
//        }
    }

    @Override
    public void onDestroy() {
        Log.e("f6===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];

                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);

                if("温度".equals(tv.getText())){
                    tempObj = obj;
                    float value = (float)obj[7];

                    tv_temp = tv;


                    if(temp>22){
                        tv_temp.setText("HI");
                    }else if(temp<18){
                        tv_temp.setText("LO");
                    }else{
                        tv_temp.setText(value+"℃");
                    }


                }else if("风速".equals(tv.getText())){
                    windObj = obj;
                    int value = (int)obj[7];

                    tv_wind = tv;
                    tv_wind.setText(value+"");
                }

                boolean isCanClick = (boolean)obj[5];

                Log.e("tv===", name+"==="+isCanClick);

                if(isCanClick){
                    tv.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===", v+"===");

                            String name = (String)obj[2];
//                            String type2 = (String)obj[5];
//                            byte[] bytes = (byte[])obj[4];

//                            tv_wind.setText(name);
//
//                            if("1".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                gifDrawable.start();
//                            }else if("2".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_2_gif);
//                                gifDrawable.start();
//                            }else if("3".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_3_gif);
//                                gifDrawable.start();
//                            }else if("4".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_4_gif);
//                                gifDrawable.start();
//                            }else if("5".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_5_gif);
//                                gifDrawable.start();
//                            }else if("6".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_6_gif);
//                                gifDrawable.start();
//                            }else if("7".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_7_gif);
//                                gifDrawable.start();
//                            }else if("AUTO".equals(name)){
//                                iv_route.setImageResource(R.mipmap.wind_auto_gif);
//                                gifDrawable.start();
//                            }

//                            write(bytes); //TODO
                        }
                    });

                    tv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {


                            return false;
                        }
                    });
                }
            }else if("gif".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                gifObj = obj;
                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();


                boolean isOpen = (boolean)obj[3];
                if(isOpen){
                    gifDrawable.start();
                }else{
                    iv_route.setImageResource(R.mipmap.wind_gif_off);
                    gifDrawable.stop();
                }

                v.addView(iv_route, params);
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                String name = (String)obj[5];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.mipmap.temp_round2){
                    iv.setVisibility(View.GONE);
                }else if(imgSrc==R.mipmap.s1_off){
                    iv_s1_off = iv;
                }else if(imgSrc==R.mipmap.s2_off){
                    iv_s2_off = iv;
                }else if(imgSrc==R.mipmap.s3_off){
                    iv_s3_off = iv;
                }else if(imgSrc==R.mipmap.s4_off){
                    iv_s4_off = iv;
                }else if(imgSrc==R.mipmap.s5_off){
                    iv_s5_off = iv;
                }else if(imgSrc==R.mipmap.s6_off){
                    iv_s6_off = iv;
                }else if(imgSrc==R.mipmap.s7_off){
                    iv_s7_off = iv;
                }else if(imgSrc==R.mipmap.s0_off){
                    iv_s0_off = iv;
                }
//                else if(imgSrc==R.mipmap.s1_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s1_on = iv;
//                }else if(imgSrc==R.mipmap.s2_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s2_on = iv;
//                }else if(imgSrc==R.mipmap.s3_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s3_on = iv;
//                }else if(imgSrc==R.mipmap.s4_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s4_on = iv;
//                }else if(imgSrc==R.mipmap.s5_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s5_on = iv;
//                }else if(imgSrc==R.mipmap.s6_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s6_on = iv;
//                }else if(imgSrc==R.mipmap.s7_on){
//                    iv.setVisibility(View.GONE);
//                    iv_s7_on = iv;
//                }else if(imgSrc==R.mipmap.sauto_on){
//                    iv.setVisibility(View.GONE);
//                    iv_sauto_on = iv;
//                }

                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){

                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===sixth", event.getAction()+"==="+MainActivity.curPos);

                            if(MainActivity.curPos!=4) return false;

                            int imgId = (int)obj[2];
                            int imgId2 = (int)obj[6];
                            String cmd = (String)obj[4];

                            if(event.getAction()==1){
                                iv.setImageResource(imgId);
//                                tv_wind.setText(name);

                                if(imgId==R.mipmap.reduce_off){
//                                    temp -= 0.5;
//                                    tv_temp.setText(temp+"℃");
//                                    uartJnit.getInstance().sendCmds(toCmd("6110"));
//                                    uartJnit.getInstance().sendCmds(toCmd("F411"));
                                }else if(imgId==R.mipmap.plus_off){
//                                    temp += 0.5;
//                                    tv_temp.setText(temp+"℃");

//                                    uartJnit.getInstance().sendCmds(toCmd("6210"));
                                }

//                                if(temp>22){
//                                    tv_temp.setText("HI");
//                                }else if(temp<18){
//                                    tv_temp.setText("LO");
//                                }

                                if(imgId==R.mipmap.s1_off){
//                                    iv_s1_off.setVisibility(View.VISIBLE);
////                                    iv_s1_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6410"));
//                                    uartJnit.getInstance().sendCmds(toCmd("D1E0"));
                                }else if(imgId==R.mipmap.s2_off){
//                                    iv_s2_off.setVisibility(View.VISIBLE);
////                                    iv_s2_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6510"));
                                }else if(imgId==R.mipmap.s3_off){
//                                    iv_s3_off.setVisibility(View.VISIBLE);
////                                    iv_s3_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6610"));
                                }else if(imgId==R.mipmap.s4_off){
//                                    iv_s4_off.setVisibility(View.VISIBLE);
////                                    iv_s4_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6710"));
                                }else if(imgId==R.mipmap.s5_off){
//                                    iv_s5_off.setVisibility(View.VISIBLE);
////                                    iv_s5_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6810"));
                                }else if(imgId==R.mipmap.s6_off){
//                                    iv_s6_off.setVisibility(View.VISIBLE);
////                                    iv_s6_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6910"));
                                }else if(imgId==R.mipmap.s7_off){
//                                    iv_s7_off.setVisibility(View.VISIBLE);
////                                    iv_s7_on.setVisibility(View.GONE);
//
//                                    iv_route.setImageResource(R.mipmap.wind_1_gif);
//                                    gifDrawable.start();

//                                    uartJnit.getInstance().sendCmds(toCmd("6A10"));
                                }else if(imgId==R.mipmap.s0_off){
//                                    iv_s0_off.setVisibility(View.VISIBLE);
////                                    iv_sauto_on.setVisibility(View.GONE);

//                                    iv_route.setImageResource(R.mipmap.wind_gif_off);
//                                    gifDrawable.stop();

//                                    uartJnit.getInstance().sendCmds(toCmd("6310"));
                                }

//                                uartJnit.getInstance().sendCmds(toCmd(cmd));
                                send(cmd);

//                                else if("2".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_2_gif);
//                                    gifDrawable.start();
//                                }else if("3".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_3_gif);
//                                    gifDrawable.start();
//                                }else if("4".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_4_gif);
//                                    gifDrawable.start();
//                                }else if("5".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_5_gif);
//                                    gifDrawable.start();
//                                }else if("6".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_6_gif);
//                                    gifDrawable.start();
//                                }else if("7".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_7_gif);
//                                    gifDrawable.start();
//                                }else if("AUTO".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_auto_gif);
//                                    gifDrawable.start();
//                                }


                            }else{
                                iv.setImageResource(imgId2);

//                                if(imgId==R.mipmap.plus_off){
//                                    iv.setImageResource(R.mipmap.plus_on);
//                                }else if(imgId==R.mipmap.reduce_off){
//                                    iv.setImageResource(R.mipmap.reduce_on);
//                                }else if(imgId==R.mipmap.switch_off){
//                                    iv.setImageResource(R.mipmap.switch_on);
//                                }
//
//                                if(imgId==R.mipmap.s1_off){
//                                    iv.setImageResource(R.mipmap.s1_on);
////                                    iv_s1_off.setVisibility(View.GONE);
////                                    iv_s1_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s2_off){
//                                    iv.setImageResource(R.mipmap.s2_on);
////                                    iv_s2_off.setVisibility(View.GONE);
////                                    iv_s2_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s3_off){
//                                    iv.setImageResource(R.mipmap.s3_on);
////                                    iv_s3_off.setVisibility(View.GONE);
////                                    iv_s3_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s4_off){
//                                    iv.setImageResource(R.mipmap.s4_on);
////                                    iv_s4_off.setVisibility(View.GONE);
////                                    iv_s4_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s5_off){
//                                    iv.setImageResource(R.mipmap.s5_on);
////                                    iv_s5_off.setVisibility(View.GONE);
////                                    iv_s5_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s6_off){
//                                    iv.setImageResource(R.mipmap.s6_on);
////                                    iv_s6_off.setVisibility(View.GONE);
////                                    iv_s6_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s7_off){
//                                    iv.setImageResource(R.mipmap.s7_on);
////                                    iv_s7_off.setVisibility(View.GONE);
////                                    iv_s7_on.setVisibility(View.VISIBLE);
//                                }else if(imgId==R.mipmap.s0_off){
//                                    iv.setImageResource(R.mipmap.s1_on);
////                                    iv_s0_off.setVisibility(View.GONE);
////                                    iv_sauto_on.setVisibility(View.VISIBLE);
//                                }

//                                else if("3".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_3_gif);
//                                    gifDrawable.start();
//                                }else if("4".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_4_gif);
//                                    gifDrawable.start();
//                                }else if("5".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_5_gif);
//                                    gifDrawable.start();
//                                }else if("6".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_6_gif);
//                                    gifDrawable.start();
//                                }else if("7".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_7_gif);
//                                    gifDrawable.start();
//                                }else if("AUTO".equals(name)){
//                                    iv_route.setImageResource(R.mipmap.wind_auto_gif);
//                                    gifDrawable.start();
//                                }
                            }

//                            write(bytes); //TODO

                            return true;
                        }
                    });

//                    iv.setOnClickListener(new OnClickListener() {
//
//                        @Override
//                        public void onClick(View v) {
//                            Log.e("onClick===iv", v+"===");
//
//                            int imgId = (int)obj[2];
//                            byte[] bytes = (byte[])obj[4];
//
//                            if(imgId==R.mipmap.temp_round){
//                                iv.setImageResource(R.mipmap.temp_round2);
//                                obj[2] = R.mipmap.temp_round2;
//
////                                read(bytes);  //TODO
//                            }else if(imgId==R.mipmap.temp_round2){
//                                iv.setImageResource(R.mipmap.temp_round);
//                                obj[2] = R.mipmap.temp_round;
//                            }
//                        }
//                    });
                }

            }
        }
    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==5){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

                    Log.e("f6===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

